package net.fxug.hokuriku.chocolat.helper
{
	import air.update.ApplicationUpdaterUI;
	import air.update.events.UpdateEvent;
	
	import flash.display.Screen;
	import flash.events.ErrorEvent;
	import flash.events.Event;
	
	import mx.core.IMXMLObject;
	import mx.core.WindowedApplication;
	import mx.events.FlexEvent;
	import mx.logging.Log;
	import mx.logging.targets.TraceTarget;

    /**
     * アプリケーションのメインとなるヘルパークラス
     *
     * @author shoito
     */
    public class SORAAIHelper implements IMXMLObject
    {
        private var view:WindowedApplication;
		
		private var appUpdater:ApplicationUpdaterUI = new ApplicationUpdaterUI();
		
        public function initialized(document:Object, id:String):void
        {
            this.view = document as WindowedApplication;
            this.view.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);
            this.view.addEventListener(FlexEvent.CREATION_COMPLETE, creationCompleteHandler);
        }

        private function creationCompleteHandler(event:FlexEvent):void
        {
            // debug::ロガーの出力先を設定
            Log.addTarget(new TraceTarget());
			
			checkForUpdate();
		}
		
		private function checkForUpdate():void {
			appUpdater.updateURL = "http://soraai.googlecode.com/files/update.xml";
			appUpdater.isCheckForUpdateVisible = false;
			appUpdater.addEventListener(UpdateEvent.INITIALIZED, appUpdaterInitializedHandler);
			appUpdater.addEventListener(ErrorEvent.ERROR, appUpdaterErrorHandler);
			appUpdater.initialize();
		}
		
		private function appUpdaterErrorHandler(event:ErrorEvent):void {
		}
		
		private function appUpdaterInitializedHandler(event:UpdateEvent):void {
			appUpdater.checkNow();
		}
		
        private function addedToStageHandler(event:Event):void
        {
            this.view.nativeWindow.x = (Screen.mainScreen.visibleBounds.width - this.view.nativeWindow.width) / 2;
            this.view.nativeWindow.y = (Screen.mainScreen.visibleBounds.height - this.view.nativeWindow.height) / 2;
        }
    }
}